<?php

namespace api\controllers;

use api\controllers\base\BaseController;
use api\models\QuestionForm;

class QuestionController extends BaseController
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * @return bool|mixed|string
     *
     http://api.spike.com/?r=question/set-question&user_sign=97aH87RYYmcXdNLaLqycFy+lh1EJy1T87bggwiWoy0KemGyllW14zM8sG5DQcNjXlHLLnXhTSHpkIasWMIF9
     */
    public function actionSetQuestion()
    {
        $req_param = \Yii::$app->request->post();

        $question_form = new QuestionForm();
        $user_sign = $question_form->rc_encrypt( $req_param[ 'user_sign' ],'D' );

        // $uid = $login_userinfo['uid'];
        $ip = $question_form->getClientIp();

        // TODO: 用户id校验,活动校验,时间限制
        $user_sign[ 'uid' ] = \Yii::$app->user->id;

        if( !$req_param[ 'user_sign' ] || $ip != $user_sign[ 'ip' ] ){

            $this->response2Json( false,'请使用本机尽快抢购,',101 );
        }


        $ask_list = $answer_list = array();
        $question_info = $question_form->setQuestion( $user_sign[ 'aid' ], $user_sign[ 'uid' ], $user_sign[ 'ip' ] );
        if ( !$question_info[ 'status' ] ) {
            $this->response2Json( false, '没有设置问答信息', 201 );
        }

        // 添加到购买队列
        $ret2addQueue = \Yii::$app->redis->zadd( 'by_user', $user_sign[ 'uid' ], $user_sign[ 'uid' ] );

        $this->response2Json( $question_info[ 'status' ], $question_info[ 'msg' ], 0, '', $question_info[ 'data' ] );

    }

}
